<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : bid.class.php   FILE_PATH : \\classes\application\bid.class.php
 * ....商品拍卖出价
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Wed Oct 24 18:48:10 CST 2007
 */
defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");

class BidClass extends FrameWork {
	/**
	 * 获得竞拍出价列表
	 *
	 * @param array $obj_condition
	 * @param object $obj_page
	 * @return array $product_bid_array
	 */
	function getProductBidList($obj_condition, &$obj_page){
		/**
		 * 构造查询条件
		 */
		$condition_str = $this->_conditionSearch($obj_condition);
		$product_bid_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "bid");
		return $product_bid_array;
	}
	/**
	 * 更新出价记录状态
	 *
	 * @param array $input
	 * @return boolean
	 */
	function updateProductBidState($input){
		if (intval($input["bid_id"]) > 0){
			$valueArray = array();
			$valueArray["bid_state"]	= $input["bid_state"];
			$update_rs = $GLOBALS['db']->UpdateRow(intval($input["bid_id"]), $valueArray, 'bid', 'bid_id');
			return $update_rs;
		}else {
			return false;
		}
	}
	/**
	 * 添加竞拍出价记录
	 *
	 * @param array $input
	 * @return boolean
	 */
	function saveProductBid($input){
		$valueArray = array();
		$valueArray["bid_member_id"]	= $input["txtBuyerId"];
		$valueArray["bid_p_code"]		= $input["txtPcode"];
		if ($input["max_price"] != ''){
			$valueArray["bid_max_price"]	= $input["max_price"];
		}
		$valueArray["bid_price"]		= $input["min_price"];
		$valueArray["bid_count"]		= $input["txtBuyNum"];
		$valueArray["bid_total_price"]	= $valueArray["bid_price"]*$valueArray["bid_count"];
		$valueArray["bid_receive_code"]	= $input["txtReceiveId"];
		$valueArray["bid_state"]		= $input["bid_state"];
		if("" == $input["bid_time"]){
			$valueArray["bid_time"]	= time();
		}else{
			$valueArray["bid_time"]	= $input["bid_time"];
		}
		//匿名购买
		if ('' != $input['anonymous']){
			$valueArray["bid_anonymous"]	= $input["anonymous"];
		}
		
		$insert_rs = $GLOBALS['db']->InsertRow($valueArray, 'bid', 'bid_id');
		return true;
	}

	/**
	 * 更新拍卖出价状态
	 *
	 * @param array $input
	 * @return boolean
	 */
	function updateProductBidStateInCondition($input){
		
		if ($input['bid_state'] != ''){
			$update_array["bid_state"] = $input['bid_state'];
		}else {
			$update_array["bid_state"] = "0";
		}
		
		$input["bid_old_state"] = "1";


		if("" != $input["p_code"]){
			$condition = " bid_p_code = '" . $input["p_code"] . "'";
		}

		if("" != $input["bid_old_state"]){
			$condition .= " and bid_state = '" . $input["bid_old_state"] . "'";
		}
		$update_rs = $GLOBALS['db']->UpdateRows('bid', $update_array, $condition);

		return true;
	}
	
	/**
	 * 得到代理出价记录
	 *
	 * @param array $input
	 */
	function agentBid($input){
		$agent_rs = $GLOBALS['db']->GetList("and bid_p_code = '" . $input[p_code] . "' and bid_max_price > bid_price and bid_state = '1'", $page, "bid");
		return $agent_rs;
	}
	
	/**
	 * 删除竞拍信息
	 * @author workflow Develop Team
	 * @param  
	 * @return bool
	 */
	function delBid($p_code){	
		if ($p_code){
			$result = $GLOBALS['db']->DelRow($p_code,'bid','bid_p_code');
			return $result;
		}else {
			return false;
		}
	}
	
	
	/**
	 * 构造出价记录检索条件
	 *
	 * @param object $obj_condition 查询条件对象
	 * @return string $conditionStr 返回查询条件SQL字符串
	 */
	function _conditionSearch($obj_condition){
		$conditionStr = "";

		/*搜索指定出价者出价记录*/
		if($obj_condition[bid_member_id] != ""){
			$conditionStr .= " and bid_member_id = '" . $obj_condition[bid_member_id] . "'";
		}
		/*按商品编号搜索*/
		if($obj_condition[p_code] != ""){
			$conditionStr .= " and bid_p_code = '" . $obj_condition[p_code] . "'";
		}
		/*按出价状态搜索*/
		if($obj_condition[bid_state] != ""){
			if (is_array($obj_condition[bid_state])) {
				$conditionStr .= " and ( ";
				foreach ($obj_condition[bid_state] as $v){
					$condition .= " or bid_state = '" . $v . "'";
				}
				$conditionStr .= substr($condition, 3).")";
			}else {
				$conditionStr .= " and bid_state = '" . $obj_condition[bid_state] . "'";
			}
		}

		/*列表排序方式*/
		if($obj_condition[order] == 1){
			$conditionStr .= " order by bid_time desc";
		}elseif($obj_condition[order] == 2){
			$conditionStr .= " order by bid_price desc, bid_time desc";
		}else{
			//$conditionStr .= " order by bid_time";
		}
		return $conditionStr;
	}
	
	/**
	 * 删除竞拍信息 按照竞拍ID
	 * @author workflow Develop Team
	 * @param  
	 * @return bool
	 */
	function delBidById($bid_id){	
		if ($bid_id){
			$result = $GLOBALS['db']->DelRow($bid_id,'bid','bid_id');
			return $result;
		}else {
			return false;
		}
	}

	/**
	 * 删除竞拍信息 按照会员ID
	 * @author workflow Develop Team
	 * @param  
	 * @return bool
	 */
	function delBidByMemberId($bid_member_id){	
		if ($bid_member_id){
			$result = $GLOBALS['db']->DelRow($bid_member_id,'bid','bid_member_id');
			return $result;
		}else {
			return false;
		}
	}
	
	/**
	 * 更新竞拍信息
	 * @author workflow Develop Team
	 * @param  
	 * @return bool
	 */
	function updateBid($input){
		if(is_array($input)){
			$value_array = array();
			foreach($input as $k => $v){
				$value_array[$k] = $v;
			}
			$update_rs = $GLOBALS['db']->UpdateRow(intval($input["bid_id"]), $value_array, 'bid', 'bid_id');
			return $update_rs;
		}else{
			return false;
		}
	}
}
?>